package com.syni.mdd.yhd.common.all.controller.portBus;

/**
 * @className BmsBusinessController
 * @description TOO
 * @Author cfx
 * @DATE 2019/4/15 14:25
 * @VERSION 1.0
 **/

import com.ijpay.core.enums.SignType;
import com.ijpay.core.kit.HttpKit;
import com.ijpay.core.kit.WxPayKit;
import com.ijpay.wxpay.WxPayApiConfigKit;
import com.syni.mdd.sjb.common.component.utils.common.utils.ParamsMap;
import com.syni.mdd.sjb.common.component.utils.common.utils.ResultMapHelper;
import com.syni.mdd.sjb.common.component.utils.common.annotations.CheckParams;
import com.syni.mdd.sjb.common.component.utils.common.annotations.EntranceCheck;
import com.syni.mdd.yhd.common.all.constants.Constants;
import com.syni.mdd.yhd.common.all.constants.ResultCode;
import com.syni.mdd.yhd.common.all.constants.ResultMsg;
import com.syni.mdd.yhd.common.all.dto.requestDto.business.BusinessReDto;
import com.syni.mdd.yhd.common.all.entity.BmsBusiness;
import com.syni.mdd.yhd.common.all.service.BmsBusinessService;
import com.syni.mdd.yhd.common.all.service.BmsCommonService;
import com.syni.mdd.yhd.common.all.service.BmsPortOrderLogService;
import com.syni.mdd.yhd.common.all.utils.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

@RestController
@RequestMapping("/portBus")
public class PortBusController {

    private Logger log = LoggerFactory.getLogger(this.getClass());

    @Autowired
    BmsPortOrderLogService bmsPortOrderLogService;

    @PostMapping("/getOrder")
    @CheckParams(params = {"orderType","orderId","openId"})
    public Map<String,Object> getOrder(@RequestBody ParamsMap paramsMap){
        String orderType = paramsMap.getString("orderType");
        String orderId = paramsMap.getString("orderId");
        String openId = paramsMap.getString("openId");
        //rpc获取口岸巴士订单
        return bmsPortOrderLogService.getPortBusOrder(openId,orderId,orderType);
    }

    /**
     * 异步通知
     */
    @RequestMapping(value = "/payNotify", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public synchronized String payNotify() {
        HttpServletRequest request = RequestParamUtils.getRequest();
        String xmlMsg = HttpKit.readData(request);
        log.info("支付通知=" + xmlMsg);
        Map<String, String> params = WxPayKit.xmlToMap(xmlMsg);

        String returnCode = params.get("return_code");

        // 注意重复通知的情况，同一订单号可能收到多次通知，请注意一定先判断订单状态
        // 注意此处签名方式需与统一下单的签名类型一致
        if (WxPayKit.verifyNotify(params, WxPayApiConfigKit.getWxPayApiConfig().getPartnerKey(), SignType.HMACSHA256)) {
            if (WxPayKit.codeIsOk(returnCode)) {
                // 更新订单信息
                bmsPortOrderLogService.payNotify(params);
                // 发送通知等
                Map<String, String> xml = new HashMap<String, String>(2);
                xml.put("return_code", "SUCCESS");
                xml.put("return_msg", "OK");
                return WxPayKit.toXml(xml);
            }
        }
        return null;
    }


    /**
     * 同步退款
     */
    @RequestMapping(value = "/refundNotify", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public Map<String,Object> refundNotify() {
        ParamData paramData = new ParamData();
        String orderType = paramData.getString("orderType");
        String orderId = paramData.getString("orderId");
        String apiKey = paramData.getString("apiKey");
        String sign = paramData.getString("sign");

        //校验参数是否正确
        Map<String,Object> paramsMap = new LinkedHashMap<>();
        paramsMap.put("orderType",orderType);
        paramsMap.put("orderId",orderId);
        paramsMap.put("apiKey",apiKey);
        if(!bmsPortOrderLogService.checkPortRpcApiKey(apiKey)){
            return ResultMapHelper.failure("apiKey错误");
        }
        if(!bmsPortOrderLogService.checkPortRpcRequest(paramsMap,sign)){
            return ResultMapHelper.failure("签名错误");
        }
        //同步退款
        if(!bmsPortOrderLogService.refundOrder(orderId,orderType)){
            return ResultMapHelper.result(ResultCode.CODE_ERROR,"退款失败");
        }
        return ResultMapHelper.success(null, ResultMsg.REFUND_SUCCESS);
    }



    /**
     * 异步通知
     */
    @RequestMapping(value = "/test", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public String test() {
        Map<String,String> params = new LinkedHashMap<>();
        params.put("out_trade_no","KABS19112821514200");
        params.put("transaction_id","4200000430201911266077707606");

        // 更新订单信息
        bmsPortOrderLogService.payNotify(params);
        return "success";
    }
}
